var http = require('http')
var url=require('url')
var fs=require('fs')

var querystring = require('querystring');


http.createServer(function (req,res) {

    var urlObj=url.parse(req.url,true);
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});
    var postData='';



    if (urlObj.pathname=='/login'){
        fs.readFile('template/login.post.html', function (err, data) {
            res.end(data.toString());
        })
    }

    if (urlObj.pathname=='/postLogin'){
        /*post请求以流（stream）的形式传递数据*/
        req.on('data',function (chuck) {
            postData+=chuck;
        })

        req.on('end',function () {
             postData=querystring.parse(postData);
             if(postData.username=="admin"){
                 fs.readFile('template/index.html', function (err, data) {
                     res.end(data.toString());
                 })
             }
            /* console.log(postData);
             res.end();*/
        })
    }

}).listen(3000)


console.log('the server is running at port 3000');
